define(['jquery', 'bootstrap', 'backend', 'table', 'form','template'], function ($, undefined, Backend, Table, Form,Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'kaoq/course/index' + location.search,
                    add_url: 'kaoq/course/add',
                    edit_url: 'kaoq/course/edit',
                    del_url: 'kaoq/course/del',
                    multi_url: 'kaoq/course/multi',
                    table: 'course',
                }
            });

            var table = $("#table");
            Template.helper("Moment", Moment);
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                templateView: true,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name')},
                        {field: 'grade.college', title: __('Grade.college')},
                        {field: 'grade.name', title: __('Grade.name')},

                        // {field: 'class_id', title: __('Class_id')},

                        {field: 'teacher_id', title: __('Teacher_id')},
                        {field: 'startTime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},


                        {field: 'pid', title: __('Pid')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: false,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: false,
                //分页大小
                pageSize: 12
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //开始考勤
            $(document).on("click", ".startRfid", function () {
               var openurl ="kaoq/course/attendance?ids="+$(this).data('id');
                Fast.api.ajax({
                    url:'/index/Qiandao/startLearn',
                    data:{course_id:$(this).data('id')}
                }, function(){
                        //成功的回调
                    top.Fast.api.open(openurl,'考勤列表');
                        return ;
                    }
                    );
                //在templateView的模式下不能调用table.bootstrapTable('getSelections')来获取选中的ID,只能通过下面的Table.api.selectedids来获取
                // Fast.api.msg("操作成功");
                // Toastr.success("成功"+$(this).data('id'));
                // Layer.msg($(this).data('id'));
            });

            //结束考勤
            $(document).on("click", ".closeRfid", function () {
                Fast.api.ajax({
                    url:'/index/Qiandao/closeLearn',
                    data:{course_id:$(this).data('id')}
                });
            });


        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        attendance:function(){
            var crouse_id = Config.crouse_id;
            var websocket = Config.websocket;
            console.log(crouse_id);
            console.log(websocket);
            Toastr.success(websocket);
            // 打开一个 web socket
            var ws = new WebSocket(websocket);

            ws.onopen = function()
            {
                var bind ="{\"type\": \"init\",\"pid\":"+crouse_id+"}";
                // Web Socket 已连接上，使用 send() 方法发送数据
                ws.send(bind);
            };

            ws.onmessage = function (evt)
            {
                var received_msg = evt.data;
                // alert("数据已接收...");
                console.log(received_msg);
                var html ='<p class="bg-success">'+received_msg+'</p>';
                $("#add").append(html);
                Toastr.success(received_msg);
            };

            ws.onclose = function()
            {
                Toastr.success("连接已关闭");
            };
        },
        api: {
            bindevent: function () {

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});